import * as _ from "lodash"
import { camelCase } from "../case"
import httpClient from "../httpclient"

export class DailyPage {
  public date: string
  public viewCount: number
  public amount: number
}

export class PageQuery {

  private params = {}

  public storeId(storeId: string): PageQuery {
    this.params["store_id"] = storeId
    return this
  }

  public date(date: string | number): PageQuery {
    this.params["date"] = date
    return this
  }

  public dateFrom(dateFrom: string | number): PageQuery {
    this.params["date_from"] = dateFrom
    return this
  }

  public dateTo(dateTo: string | number): PageQuery {
    this.params["date_to"] = dateTo
    return this
  }
}

export class TotalPages {
  public viewCount: number
}

export class PageReportV1 {

  public static createPageQuery() {
    return new PageQuery()
  }

  public static queryDailyPages(query: PageQuery): Promise<DailyPage[]> {
    return httpClient.get("/v1/reports/daily-pages", {
      params: query["params"],
    })
      .then(({ data }) => data)
      .then(camelCase)
  }

  public static queryTotalPages(query: PageQuery): Promise<TotalPages> {
    return httpClient.get("/v1/reports/total-pages", {
      params: query["params"],
    })
      .then(({ data }) => data)
      .then(camelCase)
      .then(data => _.assign(new TotalPages(), data))
  }
}
